import { Inject, Controller, Get, Query, Post, Body, App } from "@midwayjs/core";
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service.js';
// const  {exec}  =require( 'child_process')
import {exec} from 'child_process'
import fs from 'fs'
import { Application } from "../interface.js";
import { app_fun_dto } from "../models/app_fun.dto.js";
import { getdb, getsqlit_db } from "../config/initdb.js";
import { okres } from "../config/resUtil.js";
import { ApiProject } from "../models/ApiProject.dto.js";

@Controller('/api')
export class ApiprojectControler {
  @Inject()
  ctx: Context;
  @App('koa')
  app: Application;

  @Post("/apiproject/list")
  async apiprojectlist(){
    var db =  this.app.db;
    // var inert_result=await knex('tb_books').insert({title: 'Slaughterhouse Five'})
    // var pageNum = body?.pageSize || 10;
    // var tableName = body?.tableName;
    // var mainId = body?.mainId;
    // var tableproperty = body?.tableproperty;
    // var keyvalue = body?.keyvalue;
    // var offset = ((body?.pageNum || 1) - 1) * pageNum;
    var list=await db('apiproject').where('del_state',1).select()
    return okres(list);
  }
  @Post("/apiproject/add")
  async add(@Body() body :ApiProject ){
    var db =  this.app.db;
    // var inert_result=await knex('tb_books').insert({title: 'Slaughterhouse Five'})
    // var pageNum = body?.pageSize || 10;
    // var tableName = body?.tableName;
    // var mainId = body?.mainId;
    // var tableproperty = body?.tableproperty;
    // var keyvalue = body?.keyvalue;
    // var offset = ((body?.pageNum || 1) - 1) * pageNum;
    delete body.id
    //默认填充 del_state
    body.del_state=1
    var result=await db('apiproject').insert(body)
    return okres(result);
  }

  @Post("/apiproject/update")
  async update(@Body() body :ApiProject ){
    var db =  this.app.db;
    // var inert_result=await knex('tb_books').insert({title: 'Slaughterhouse Five'})
    // var pageNum = body?.pageSize || 10;
    // var tableName = body?.tableName;
    // var mainId = body?.mainId;
    // var tableproperty = body?.tableproperty;
    // var keyvalue = body?.keyvalue;
    // var offset = ((body?.pageNum || 1) - 1) * pageNum;
    var result=await  db('apiproject')
      .where('id', '=', body.id)
      .update(body)
    return okres(result);
  }

  @Get("/apiproject/debyid")
  async debyid(@Query("id") id  ){
    var db =  this.app.db;
    // var inert_result=await knex('tb_books').insert({title: 'Slaughterhouse Five'})
    // var pageNum = body?.pageSize || 10;
    // var tableName = body?.tableName;
    // var mainId = body?.mainId;
    // var tableproperty = body?.tableproperty;
    // var keyvalue = body?.keyvalue;
    // var offset = ((body?.pageNum || 1) - 1) * pageNum;
    var result=await  db('apiproject')
      .where('id', '=', id)
      .update({del_state:5})
    return okres(result);
  }


}
